package edu.uoc.tfc.web.tags;



import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;



public class AjaxButtonTag extends AbstractButtonTag 
{

    private static final long serialVersionUID = 1L;




    private String formName;
    private boolean validateForm = true;
    private boolean dismissModal;
	private String onSuccess;
    private String onError;
    private String onSuccessForward;
    private String actionType;
    
    

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public boolean isValidateForm() {
		return validateForm;
	}

	public void setValidateForm(boolean validateForm) {
		this.validateForm = validateForm;
	}



	public String getFormName() {
    	return formName;
    }

	public void setFormName(String formName) {
    	this.formName = formName;
    }



    public boolean isDismissModal() {
		return dismissModal;
	}

	public void setDismissModal(boolean dismissModal) {
		this.dismissModal = dismissModal;
	}



	public String getOnSuccess() {
		return onSuccess;
	}

	public void setOnSuccess(String onSuccess) {
		this.onSuccess = onSuccess;
	}



    public String getOnError() {
		return onError;
	}

	public void setOnError(String onError) {
		this.onError = onError;
	}



	public String getOnSuccessForward() {
		return onSuccessForward;
	}

	public void setOnSuccessForward(String onSuccessForward) {
		this.onSuccessForward = onSuccessForward;
	}



	public String getStyle() {
    	return style;
    }

	public void setStyle(String style) {
    	this.style = style;
    }



	@Override
	public int doStartTag() throws JspException
	{
		try
		{
			writeButton(pageContext.getOut());
		}
		catch (Exception ex)
		{
			String message = "Error generando AjaxButton: " + this.getProperty();
			throw new JspException(message);
		}

		return SKIP_BODY;
	}



	@Override
	protected void writeButton(JspWriter out) throws Exception
	{
		write(out, "<button " + getTypeMode() + getClassMode() + getData() + ">");
		if (StringUtils.isBlank(getIconClass()))
		{
			write(out, getMessage(getLabel(), null));
		}
		else
		{
			write(
				out,
				"<div class=\"the-icons\"><i class=\""
				+ getIconClass()
				+ "\"></i> "
				+ getMessage(getLabel(), null)
				+ "</div>");
		}
		write(out, "</button>");
	}



	@Override
	protected String getData()
	{
		StringBuilder data = new StringBuilder();

		if (StringUtils.isNotBlank(getOperation()))
		{
			data.append(" data-service-url=\"" + super.getContextPath() + "\"");
			data.append(" data-operation-url=\"" + getOperation() + "\"");
		}

		if (StringUtils.isNotBlank(getId()))
		{
			data.append(" id=\"" + getId() + "\"");
		}

		if (StringUtils.isNotBlank(getModalClass()))
		{
			data.append(" data-toggle=\"" + getModalClass() + "\"");
			setTargetDiv("modal-body");
		}

		if (StringUtils.isBlank(getTargetDiv()))
		{
			setTargetDiv("content");
		}

		data.append(" data-target-div=\"" + getTargetDiv() + "\"");
		data.append(" data-bytoken=\"" + getBytoken() + "\"");

		String actionScript = getDataActionScript();
		if (StringUtils.isNotBlank(actionScript))
		{
			data.append(" data-action=\"" + actionScript + "\"");
		}		
		
		if (StringUtils.isNotBlank(getFormName()))
		{
			data.append(" data-form-name=\"" + getFormName() + "\"");
			data.append(" data-form-validate=\"" + isValidateForm() + "\"");
		}

		if (isDismissModal())
		{
			data.append(" data-success-close=\"true\"");
		}

		if (StringUtils.isNotBlank(getOnSuccess()))
		{
			data.append(" data-success=\"" + getOnSuccess() + "\"");
		}

		if (StringUtils.isNotBlank(getOnSuccessForward()))
		{
			data.append(" data-success-forward=\"" + getOnSuccessForward() + "\"");
		}

		if (StringUtils.isNotBlank(getOnError()))
		{
			data.append(" data-error=\"" + getOnError() + "\"");
		}
		
	    return data.toString();
    }



	@Override
	protected String getClassMode()
	{
		return " class=\"btn " + getStyle() + " emkt-ajax\"";
    }



	@Override
	protected String getTypeMode()
	{
		return " type=\"submit\"";
    }


	private String getDataActionScript()
	{
		String result = null;
		if (StringUtils.isBlank(actionType))
		{
			String selectFunction = pageContext.getRequest().getParameter("select-function");
			if (StringUtils.isNotBlank(selectFunction))
			{
				result = selectFunction;
			}
		}
		else
		{
			result = getActionType();
		}
		return result;
	}
}
